﻿/// <reference path="class.js" />
/// <reference path="ui.js" />
/// <reference path="data-persister.js" />
/// <reference path="jquery-2.0.2.js" />
var uiController = (function () {
    var onlineUsers;
    var eventsAttached = false;

    var Controller = Class.create({
        init: function (url) {
            this.persister = dataPersister.getPersister(url);
        },

        loadUI: function (selector) {
            var self = this;
            if (self.persister.isLoggedIn()) {

                self.persister.user.users(function (users) {
                    $(selector).html(ui.getLoggedInForm(self.persister.getNickname(), users));
                });
            }
            else {
                $(selector).html(ui.getLoginForm);
            }

            if (!eventsAttached) {
                eventsAttached = true;
                this.attachEvents(selector);
            }
        },

        attachEvents: function (selector) {
            var self = this;
            var scoresVisible = false;
            var messagesVisible = false;

            $(selector).on("click", "#login-button", function () {
                var username = $("#login-username-input").val();
                var password = $("#login-password-input").val();

                var user = {
                    username: username,
                    password: password
                }

                self.persister.user.login(user, function () {
                    self.loadUI("#wrapper");
                }, function (error) {
                    var message = JSON.parse(error.responseText);
                    $("#error-container").html(message.Message);
                    $("#error-container").show();
                    $("#error-container").fadeOut(3000);
                });
                return false;
            });

            $(selector).on("click", "#register-button", function () {
                var username = $("#register-username-input").val();
                var password = $("#register-password-input").val();

                var user = {
                    username: username,
                    password: password
                }

                self.persister.user.register(user, function () {
                    self.loadUI("#wrapper");
                }, function (error) {
                    var message = JSON.parse(error.responseText);
                    $("#error-container").html(message.Message);
                    $("#error-container").show();
                    $("#error-container").fadeOut(3000);
                });
                return false;
            });

            $(selector).on("click", "#send-message", function () {
                self.persister.message.send(

                   {
                       "text": $('#message-to-send').val(),
                       "sendername": self.persister.getNickname(),
                       "channel": "monster"
                   },

                   function (data) {
                       $('#message-to-send').val("");
                   },

                   function (err) {
                       console.log("message not send... -> " + err);
                   });
            });

            $("#big-wrapper").on("click", "#logout-button", function () {
                self.persister.user.logout(function () {
                    self.loadUI(selector);
                    $("#hello-user-holder").html('');

                }, function (error) {
                    $(selector).append(error.responseText);
                });
                return false;
            });

            //$("#message-to-send").keypress(function () {
            //    console.log("Handler for .keypress() called.");
            //});
            $(document).ready(function () {
                $("#wrapper").keypress(function (e) {
                    if (e.which == 13) {
                        self.persister.message.send(
                          {
                              "text": $('#message-to-send').val(),
                              "sendername": self.persister.getNickname(),
                              "channel": "monster"
                          },

                          function (data) {
                              $('#message-to-send').val("");
                          },

                          function (err) {
                              console.log("message not send... -> " + err);
                          });
                    }
                });
            });
        },
    })

    return {
        getController: function (url) {
            return new Controller(url);
        }
    }
}());

$(function () {
    var controller = uiController.getController("http://monsterchat.apphb.com/api/");
    controller.loadUI("#wrapper");
});
